import { GRID_SIZE, ATOM_NODE, CONTAINER_NODE, NODE_PORTS } from '../config'
import { assign, max, isArray, toLower } from 'lodash'
import {
  StartIcon,
  EndIcon,
  ConveyorIcon,
  FactoryIcon,
  WorkshopIcon,
  ProductionLineIcon,
  StorageAreaIcon,
  StationIcon
} from '@/assets/icons'
import { INode } from '@/types'

class Node {
  label: string = ''
  type: string = ''
  width: number = 0
  height: number = 0
  enable: boolean = true
  // 连结桩
  ports: Record<string, any> = {}
  markup: Record<string, any>[] = []
  attrs: Record<string, any> = {}
  tools: Record<string, any>[] = []
  data: Record<string, any> = {}

  constructor(config: INode) {
    const defaultConfig = config.container ? CONTAINER_NODE : ATOM_NODE
    assign(this, {
      ...defaultConfig,
      ports: NODE_PORTS,
      ...config
    })
    this.width = max([this.width, GRID_SIZE]) || 0
    this.height = max([this.height, GRID_SIZE]) || 0

    // 采取两种数据格式的支持
    // 1.如果是数组，则使用默认节点配置，用户可以选择显示哪几个port（top,right,bottom,left)。默认四个全部显示
    // 2.如果是对象，则认为是用户自定义，直接覆盖
    if (isArray(config.ports)) {
      this.ports = {
        ...NODE_PORTS,
        items: config.ports.map((pos) => {
          return {
            group: toLower(pos)
          }
        })
      }
    }
  }
}

export const NODE_TYPE = {
  // 输入源
  START: 'start',
  // 终点
  END: 'end',
  // 工厂
  FACTORY: 'factory',
  // 车间
  WORKSHOP: 'workshop',
  // 产线
  PRODUCTION_LINE: 'productionLine',
  // 存储区
  STORAGE_AREA: 'storageArea',
  // 工位
  STATION: 'station',
  // 传送带
  CONVEYOR: 'conveyor'
}

const START: INode = {
  type: NODE_TYPE.START,
  label: '输入源',
  ports: ['right'],
  icon: StartIcon,
  enable: true
}

const END: INode = {
  type: NODE_TYPE.END,
  label: '终点',
  ports: ['left'],
  icon: EndIcon,
  enable: true
}

const CONVEYOR: INode = {
  type: NODE_TYPE.CONVEYOR,
  label: '传送带',
  width: GRID_SIZE * 8,
  className: 'icon-conveyor',
  icon: ConveyorIcon,
  enable: true
}

const FACTORY: INode = {
  type: NODE_TYPE.FACTORY,
  label: '工厂',
  // resize: true,
  container: true,
  icon: FactoryIcon,
  enable: true
}

const WORKSHOP: INode = {
  type: NODE_TYPE.WORKSHOP,
  label: '车间',
  // resize: true,
  container: true,
  icon: WorkshopIcon,
  enable: true
}

const PRODUCTION_LINE: INode = {
  type: NODE_TYPE.PRODUCTION_LINE,
  label: '产线',
  container: true,
  icon: ProductionLineIcon,
  enable: true
}

const STORAGE_AREA: INode = {
  type: NODE_TYPE.STORAGE_AREA,
  label: '存储区',
  container: true,
  icon: StorageAreaIcon,
  enable: true
}

const STATION: INode = {
  type: NODE_TYPE.STATION,
  label: '工位',
  icon: StationIcon,
  enable: true
}

export const NODE_SOURCES = (() => {
  return [
    {
      label: '流程控制',
      children: [START, END]
    },
    {
      label: '布局节点',
      children: [FACTORY, WORKSHOP, PRODUCTION_LINE, STORAGE_AREA, STATION, CONVEYOR]
    }
  ].map((item) => {
    return {
      ...item,
      children: item.children
        .map((node) => {
          return new Node(node)
        })
        .filter((node) => node.enable)
    }
  })
})()
